//
//  ResumeAppDelegate.m
//  Resume
//
//  Created by Jeffrey Palm on 11/9/09.
//  Copyright jeffpalm.com 2009. All rights reserved.
//

#import "ResumeViewController.h"
#import "ResumeAppDelegate.h"
#import "AlertPrompt.h"
#import <MediaPlayer/MediaPlayer.h>

#define kNumTasks 4
#define kProgressInc 1.0 / kNumTasks

NSString *kUserKey = @"userKey";

@implementation ResumeViewController

@synthesize statusLabel;
@synthesize spinner;
@synthesize progress;
@synthesize artistLabel;
@synthesize albumLabel;
@synthesize trackLabel;
@synthesize app;

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
	if (buttonIndex != [alertView cancelButtonIndex]) {
		NSString *user = [(AlertPrompt *)alertView enteredText];
		
		// since no default values have been set (i.e. no preferences file created), create it here		
		NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
																 user, kUserKey, nil];
		NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
		[defaults registerDefaults:appDefaults];
		[defaults synchronize];
		[app setUserAndStart:user];
	}
}

- (void)getUserAndStart {
	
	app = (ResumeAppDelegate*)[[UIApplication sharedApplication] delegate];

	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	
	NSString *user = [defaults stringForKey:kUserKey];
	
	NSLog(@"user:%@", user);
	
	if (user == nil) {
		// no default values have been set, create them here based on what's in our Settings bundle info
		//
		NSString *pathStr = [[NSBundle mainBundle] bundlePath];
		NSString *settingsBundlePath = [pathStr stringByAppendingPathComponent:@"Settings.bundle"];
		NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"Root.plist"];
		
		NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];
		NSArray *prefSpecifierArray = [settingsDict objectForKey:@"PreferenceSpecifiers"];
		
		NSString *userDefault;
		NSDictionary *prefItem;
		for (prefItem in prefSpecifierArray) {
			
			NSString *keyValueStr = [prefItem objectForKey:@"Key"];
			id defaultValue = [prefItem objectForKey:@"DefaultValue"];
			
			if ([keyValueStr isEqualToString:kUserKey]) {
				userDefault = defaultValue;
			}
		}
		
		AlertPrompt *prompt = [AlertPrompt alloc];
		prompt = [prompt initWithTitle     : @"User name"
										 message           : @"Enter the user name"
										 delegate:self 
										 cancelButtonTitle : @"Cancel" 
										 okButtonTitle     : @"OK"];
		[prompt show];
	  [prompt release];
	} else {
		[app setUserAndStart:user];
	}
	[pool release];

}	

- (void)startWorkingThread:(NSString*)msg {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[spinner startAnimating];
	[statusLabel setText:msg];
	[self.view setNeedsDisplay];
	[pool release];
}

- (void)setStatusThread:(NSString*)msg {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[statusLabel setText:msg];
	[self.view setNeedsDisplay];
	[pool release];
}

- (void)doneTaskThread:(id)param {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	progress.progress += kProgressInc;
	[self.view setNeedsDisplay];
	[pool release];
}

- (void)noteSongThread:(MPMediaItem*)song {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[artistLabel	setText:[song valueForProperty:MPMediaItemPropertyArtist]];
	[albumLabel		setText:[song valueForProperty:MPMediaItemPropertyAlbumTitle]];
	[trackLabel		setText:[song valueForProperty:MPMediaItemPropertyTitle]];
	[pool release];
}
										

- (void)startWorking:(NSString*)msg {
	[NSThread detachNewThreadSelector:@selector(startWorkingThread:) toTarget:self withObject:msg];	
}

- (void)setStatus:(NSString*)msg {
	[NSThread detachNewThreadSelector:@selector(setStatusThread:) toTarget:self withObject:msg];	
}

- (void)doneTask {
	[NSThread detachNewThreadSelector:@selector(doneTaskThread:) toTarget:self withObject:nil];	
}

- (void)noteSong:(MPMediaItem*)song {
	[NSThread detachNewThreadSelector:@selector(noteSongThread:) toTarget:self withObject:song];	
}

 - (void)viewDidLoad {
	 [super viewDidLoad];
	 [spinner setHidesWhenStopped:TRUE];
	 progress.progress = 0.0;
 }
 
- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
	[super dealloc];
	[statusLabel dealloc];
	[progress dealloc];
	[spinner dealloc];
}



@end

